#!/bin/bash
# 检查脚本是否以root权限运行
if [[ $EUID -ne 0 ]]; then
    echo "请以root权限运行此脚本。"
    exit 1
fi
#创建用户并设置密码
name=elites
pass='Elzz2023@163.com'
#echo "you are setting username : ${name}"
#echo "you are setting password : $pass for ${name}"
#调用name变量创建用户
useradd $name -m
if [ $? -eq 0 ];then
    echo -e "\033[32m用户${name}:创建成功\033[0m"
else
    echo -e "\033[31m用户${name}:创建失败\033[0m"
    exit 1
fi
echo $pass | passwd $name --stdin  &> /dev/null
if [ $? -eq 0 ];then
    echo -e "\033[32m用户${name}:密码设置成功!!!"
else
    echo -e "\033[31m用户${name}:密码密码设置失败\033[0m"
fi

#追加用户至sudoers设置sudo免密调用
#sed -i '/^root.*ALL=(ALL).*ALL/a\'$name'\tALL=(ALL) \tNOPASSWD:ALL' /etc/sudoers
echo "$name ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/$name

# 禁止root 登录
sed -i '/PermitRootLogin/a\PermitRootLogin no' /etc/ssh/sshd_config
systemctl restart sshd
